import {MarkdownParser} from 'prosemirror-markdown'
import markdownit from 'markdown-it'

const md = markdownit()

export function createTiptapMarkdownParser(schema) {
  return new MarkdownParser(schema, md, {
    // ✅ 添加 fence 支持
    fence: {
      block: 'codeBlock',
      getAttrs: token => ({
        language: token.info || null,
      }),
    },

    // 仍保留 code_block（兼容缩进风格）
    code_block: {
      block: 'codeBlock',
      getAttrs: () => ({
        language: null,
      }),
    },

    list_item: {
      block: 'listItem',
    },

    bullet_list: {
      block: 'bulletList',
    },

    ordered_list: {
      block: 'orderedList',
      getAttrs: token => ({
        order: +token.attrGet('start') || 1,
      }),
    },
    heading: {
      block: 'heading',
      getAttrs: token => ({
        level: +token.tag.slice(1), // h1 -> 1
      }),
    },
    paragraph: {
      block: 'paragraph',
    },
    strong: {
      mark: 'bold',
    },
    hr: {
      node: 'horizontalRule',  // 添加对水平分割线的支持
    },
    code_inline: {
      mark: 'code',
    },
    em: {
      mark: 'italic',
    },
    hardbreak: {
      node: 'hardBreak'
    },
     blockquote_open: {
      block: 'blockquote',
      noCloseToken: true // 标记为不需要闭合标签
    },
  })
}
